package ui.panel;

import cerebrum.common.ConfigPropertiesUtil;
import cerebrum.controller.WallpaperCerebrum;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Paint;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import ui.Controller;
import ui.Window;

import java.io.IOException;
import java.net.URL;

public class Welcome extends Window {
    public Welcome() throws IOException {
        URL resource = getClass().getResource("/fxml/Welcome.fxml");
        root = FXMLLoader.load(resource);

        //缩放图片到指定尺寸
        ((ImageView) $("welcome-image")).setFitWidth(450);
        ((ImageView) $("welcome-image")).setFitHeight(460);

        Scene scene = new Scene(root, 460, 450);
        scene.setFill(Paint.valueOf("#ffffff00"));
        initStyle(StageStyle.TRANSPARENT);
        setScene(scene);
        setTitle("IceoLogy壁纸");
        move();
        setIcon();
        //逐渐变透明
        Timeline timeline = new Timeline();
        KeyFrame key = new KeyFrame(Duration.millis(4800),
                new KeyValue(root.opacityProperty(), 0.3));
        timeline.getKeyFrames().add(key);

        //关闭此欢迎页面，显示设置页面
        timeline.setOnFinished((ae) -> setUpShow(this));
        timeline.play();
    }

    //关闭此欢迎页面，显示设置页面
    private void setUpShow(Welcome welcome) {
        //关闭欢迎页面
        welcome.close();
        //获取用户是否已经进行设置
        Boolean isSetUp = Boolean.valueOf(ConfigPropertiesUtil.get("is_user_set_up"));
        //没有设置就展开设置界面,设置了进入程序执行
        if (isSetUp) {
            //设置了就打开后台等待下载壁纸替换壁纸
            WallpaperCerebrum.startRun(false);
        } else {
            Controller.getSetUp().show();
        }

        //设置页面始终最靠前
//        Controller.getSetUp().setAlwaysOnTop(true);
    }
}
